

export default {
  
  namespaced: true,
  
  state: () => ({
    address: JSON.parse( uni.getStorageSync('address') || '{}' ),
    
    // 用户登录凭证
    token: uni.getStorageSync('token') || '',
    
    // 用户微信信息
    userinfo: JSON.parse( uni.getStorageSync('userinfo') || '{}' ),
    
    // 跳转对象{openType, from}
    redirectInfo: null
  }),
  
  mutations: {
    // 更新收货地址
    updateAddress(state, address) {
      state.address = address
      
      this.commit('m_user/saveAddressToStorage')
    },
    // 保存地址到本地
    saveAddressToStorage(state){
      uni.setStorageSync('address', JSON.stringify(state.address))
    },
    
    // 保存用户信息
    saveUserInfo(state, userinfo){
      state.userinfo = userinfo
      
      // 保存用户信息到本地
      this.commit('m_user/saveUserInfoToStorage')
    },
    // 保存用户信息到本地
    saveUserInfoToStorage(state){
      uni.setStorageSync('userinfo', JSON.stringify(state.userinfo))
    },
    
    // 保存token
    updateToken(state, token){
      state.token = token
      
      // 保存token信息到本地
      this.commit('m_user/saveTokenToStorage')
    },
    // 保存token到本地
    saveTokenToStorage(state){
      uni.setStorageSync('token', state.token)
    },
    
    // 跳转信息
    updateRedirectInfo(state, redirectInfo){
      state.redirectInfo = redirectInfo
    }
    
  },
  
  getters: {
    addstr(state){
      if(JSON.stringify(state.address) ==='{}') return ''
      
      return state.address.provinceName + state.address.cityName + state.address.countyName + state.address.detailInfo
    }
  }
  
}